<template>
  <div class="wrap">
    <div class="list">
      <div class="item">
        <input type="text" placeholder="请输入姓名" v-model="name" />
      </div>
      <div class="item">
        <input type="text" placeholder="请输入手机号" v-model="tel" />
      </div>
      <div class="item" @click="state = true">
        <p>{{address?address:'请选择地址'}}</p>
      </div>
      <div class="item">
        <input type="text" placeholder="请输入街道地址" v-model="content" />
      </div>
      <div class="btm">
        <img
          :src="bok?require('../assets/select.png'):require('../assets/radio.png')"
          aplt
          @click="bok = !bok"
        />
        <p>保存到地址簿</p>
      </div>
      <div class="textParde">
        <textarea name id="areat" v-model="textContent" cols="30" rows="10"></textarea>
        <p @click="analysis">识别解析</p>
      </div>
      <p class="btn" @click="next">确定</p>
    </div>
    <van-popup v-model="state" position="bottom" :style="{ height: '40%' }">
      <van-area :area-list="areaList" @confirm="confirm" @cancel="state = false" />
    </van-popup>
  </div>
</template>
<style lang="scss" scoped>
.list {
  padding-top: 0.8rem;
}
.item {
  height: 1.2rem;
  width: 8.16rem;
  margin: 0 auto;
  border-bottom: 1px solid #e5e5e5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  input,
  p {
    color: #999;
    font-size: 0.32rem;
    line-height: 1.2rem;
    outline: none;
    border: none;
    flex: 2;
    text-align: left;
  }
  input::-webkit-input-placeholder {
    color: #999;
    font-size: 0.32rem;
  }
  .first {
    height: 0.586667rem;
    border-left: 1px solid #e5e5e5;
    width: 1.066667rem;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .add-icon {
    width: 0.44rem;
    height: 0.493333rem;
    display: block;
  }
}
.btm {
  display: flex;
  align-items: center;
  height: 1.2rem;
  width: 8.16rem;
  margin: 0 auto;
  img {
    width: 0.453333rem;
    height: 0.453333rem;
    display: block;
    margin-right: 0.266667rem;
  }
  p {
    color: #999;
    font-size: 0.32rem;
  }
}
.textParde {
  margin: 0.4rem auto 0;
  width: 8.16rem;
  height: 2.933333rem;
  border: 1px solid #e5e5e5;
  border-radius: 0.266667rem;
  #areat {
    border: none;
    outline: none;
    width: 90%;
    display: block;
    height: 60%;
    margin: 10px auto 10px;
  }
  p {
    color: #3fd9c9;
    font-size: 0.293333rem;
    text-align: right;
    display: block;
    margin-right: 15px;
  }
}
.btn {
  width: 8.16rem;
  height: 1.066667rem;
  margin: 4rem auto 0;
  background: #3fd9c9;
  color: #fff;
  font-size: 0.48rem;
  text-align: center;
  line-height: 1.066667rem;
  border-radius: 0.266667rem;
}
</style>
<script>
import { Area, Dialog } from "vant";
import areaList from "../utils/area";
import { addressSave } from "@/utils/interface";

export default {
  data() {
    return {
      value: [],
      address: "",
      state: false,
      areaList: null,
      name: "",
      tel: "",
      province: "",
      city: "",
      district: "",
      content: "",
      isDefault: 0,
      textContent: "",
      bok: true,
      bid:1
    };
  },
  created() {
    document.title = "填写地址";
    this.areaList = areaList;
    if(this.$route.query.id){
      this.bid = this.$route.query.id
    }
  },
  methods: {
    next() {
      if (!this.name) {
        Dialog.alert({
          message: "请输入昵称"
        });
        return false;
      }
      if (!/^1[3456789]\d{9}$/.test(this.tel)) {
        Dialog.alert({
          message: "手机号码有误，请重填"
        });
        return false;
      }
      if (!this.address) {
        Dialog.alert({
          message: "请选择地址"
        });
        return false;
      }
      if (!this.content) {
        Dialog.alert({
          message: "请输入详细街道地址"
        });
        return false;
      }
      if (this.bok) {
        addressSave({
          name: this.name,
          tel: this.tel,
          province: this.province,
          city: this.city,
          district: this.district,
          content: this.content,
          isDefault: this.isDefault
        }).then(res => {
         
        });
      }
      let pa = {
        name: this.name,
        tel: this.tel,
        province: this.province,
        city: this.city,
        district: this.district,
        content: this.content,
        isDefault: this.isDefault
      }
      if(this.bid==1){
         this.$store.commit("setAddress3", pa);
      }else{
        this.$store.commit("setAddress", pa);
      }
      
      this.$router.go(-1)
    },
    confirm(e) {
      this.state = false;
      let a = [];
      this.province = e[0].name;
      this.city = e[1].name;
      this.district = e[1].name;
      e.map(item => {
        a.push(item.name);
      });

      this.address = a.toString();
    },
    selects() {
      this.isDefault == 1 ? (this.isDefault = 0) : (this.isDefault = 1);
    },
    analysis() {
      let regex =
        "(?<province>[^省]+省|.+自治区)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)(?<county>[^市]+市|[^县]+县|[^旗]+旗|.+区)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
      let address = this.textContent.match(regex);
      this.province = address[1];
      this.city = address[2];
      this.district = address[3];
      this.content = address[5];
      this.address = address[1] + address[2] + address[3];
    }
  }
};
</script>